package org.foundation.jala.softzip;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DialogToChoose extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel destinyLabel;
	private JLabel sourceFileLabel;
	private JTextField sourcePath;
	private JTextField destinyPath;
	private JButton selectDestinyButton;
	private JButton selectSourceButton;
	private JButton cancelButton;
	private JButton okButton;
	private JFileChooser fileChooser;
	public String sourcePathZip;
	public String destinyPathZip;
        private String frameName;
	
	public DialogToChoose(String name){
           super(name);
           this.frameName=name;
	   initComponents();	
	   this.setSize(600, 100);
	   this.setResizable(false);
	   this.setVisible(true);
	   this.setLocationRelativeTo(null);
	}
	
	private void initComponents(){

		fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		destinyLabel = new JLabel("Destiny Path");
		sourceFileLabel = new JLabel("Source File");
		sourcePath = new JTextField();
		destinyPath = new JTextField();
		selectDestinyButton = new JButton(new AbstractAction("...") {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent arg0) {
		         fileChooser.showSaveDialog(destinyLabel);
		         String path = fileChooser.getSelectedFile().getAbsolutePath();
		         destinyPath.setText(path);
		         destinyPathZip = destinyPath.getText();
			}
		});
		selectSourceButton = new JButton(new AbstractAction("...") {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				fileChooser.showOpenDialog(destinyLabel);
				String path = fileChooser.getSelectedFile().getAbsolutePath();
		         sourcePath.setText(path);
		         sourcePathZip = sourcePath.getText();
			}
		});
		cancelButton = new JButton(new AbstractAction("Cancel") {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent arg0) {
			      sourcePathZip = "";
			      destinyPathZip = "";
			      dispose();
			}
		});
		okButton = new JButton(new AbstractAction("OK") {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
                                SoftZip softzip = new SoftZip();
                                if(frameName=="Compress"){
                                    softzip.compress(sourcePathZip, destinyPathZip, 0);
                                }else{
                                    softzip.decompress(sourcePathZip, destinyPathZip);
                                }
                                dispose();
			}
		});
		this.setLayout(new GridLayout(3, 3, 5, 5));
		this.add(sourceFileLabel);
		this.add(sourcePath);
		this.add(selectSourceButton);
		this.add(destinyLabel);
		this.add(destinyPath);
		this.add(selectDestinyButton);
		this.add(new JPanel());
		this.add(cancelButton);
		this.add(okButton);
		
	}
	
	
	
}
